/*
编写 : Hexh
时间 : 2012-5-17 10:27:14
*/
using System;
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository;
using HMIS.FrameWork.Repository.Nhb;
using FsCarInfo;

namespace FsCarInfo.Core.Entity.IRespositories.Impl
{
	/// <summary>
	///	 the repository of FsCarbaseinfo
	/// </summary>
	public class FsCarbaseinfoRepositoryImpl : RepositoryNhbImpl<FsCarbaseinfo>, IFsCarbaseinfoRepository
	{
		/// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <returns>True， False</returns>
		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}

		/// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <param name="where">Where 条件</param>
		/// <returns>True， False</returns>
		public bool IsFieldExistWhere(string fieldName, string fieldValue, string id, string where)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, where);
		}
	
		/// <summary>
		/// 查询 FsCarbaseinfo
		/// </summary>
		/// <param name="query">查询条件</param>
		/// <param name="state">车辆状态</param>
		/// <param name="departmentid">所属展场</param>
		/// <param name="group">所属公司</param>
		/// <param name="start">分页开始</param>
		/// <param name="limit">分页大小</param>
		/// <param name="sort">排序条件</param>
		/// <param name="dir">排序方向</param>
		/// <param name="filters">筛选条件</param>
		/// <param name="total">返回记录条数</param>
		/// <returns>返回记录集</returns>
		public IList<FsCarbaseinfo> GetPlist(string query, string state, string departmentid, string group,  int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");
			
			// 修改查询条件
			string strQuery = string.IsNullOrEmpty(query) ? strFilter
				: "( t.ICCardUId = :query or t.KeyICCardUId = :query or t.EnginId = :query or t.FrameId = :query ) " 
					+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter);
			// state
			strQuery = string.IsNullOrEmpty(state) ? strQuery
				: " ( t.State = :state ) " + (string.IsNullOrEmpty(strQuery) ? strQuery : " and " + strQuery);
			// department
			strQuery = string.IsNullOrEmpty(departmentid) ? strQuery
				: " ( t.DepartmentId = :departmentid ) " + (string.IsNullOrEmpty(strQuery) ? strQuery : " and " + strQuery);
			// group
			strQuery = string.IsNullOrEmpty(group) ? strQuery
				: " ( t.Compgroup = :group ) " + (string.IsNullOrEmpty(strQuery) ? strQuery : " and " + strQuery);
			
			// sort
			string strSort = string.IsNullOrEmpty(sort) ? string.Empty : " order by t." + sort + " " + dir;

			// 修改关联查询条件
			string strSql = @" from FsCarbaseinfo as t 
									left join fetch t.Department "
				+ (string.IsNullOrEmpty(strQuery) ? string.Empty : " where " + strQuery);

			// 查询
			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql.Replace(" join fetch ", " join "));

			// 设置查询条件
			if (!string.IsNullOrEmpty(query))
			{
				selQuery.SetString("query", query);
				totQuery.SetString("query", query);
			}
			if (!string.IsNullOrEmpty(state))
			{
				selQuery.SetString("state", state);
				totQuery.SetString("state", state);
			}
			if (!string.IsNullOrEmpty(departmentid))
			{
				selQuery.SetString("departmentid", departmentid);
				totQuery.SetString("departmentid", departmentid);
			}
			if (!string.IsNullOrEmpty(group))
			{
				selQuery.SetString("group", group);
				totQuery.SetString("group", group);
			}

			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<FsCarbaseinfo>();
		}

		/// <summary>
		/// 根据ID类信息返回商品车
		/// </summary>
		/// <param name="q_query"></param>
		/// <returns></returns>
		public FsCarbaseinfo GetByQuery(string q_query)
		{
			var query = Session.CreateQuery(@"select t from FsCarbaseinfo as t where t.Id=:q_query or t.ICCardUId=:q_query or t.KeyICCardUId=:q_query or t.EnginId=:q_query or t.FrameId=:q_query")
				.SetString("q_query", q_query)
				.UniqueResult<FsCarbaseinfo>();
			return query;
		}
	}
}
